Do not run examples during 'cargo test'
authorBen Longbons <b.r.longbons@gmail.com>
Fri, 11 Jul 2014 20:34:50 +0000 (13:34 -0700)
committerBen Longbons <b.r.longbons@gmail.com>
Fri, 11 Jul 2014 20:41:26 +0000 (13:41 -0700)
src/bin/cargo-test.rs
src/cargo/ops/cargo_compile.rs
tests/test_cargo_test.rs

index b0e2a41a4e54598a7203af616c5dbb55f75402a0..bee0541ceeed265d3558125193532d5ca6a80981 100644 (file)
@@ -9,7 +9,6 @@ extern crate serialize;
 extern crate hammer;
 
 use std::os;
-use std::io::{UserExecute, fs};
 
 use cargo::ops;
 use cargo::{execute_main_without_stdin};
@@ -53,31 +52,17 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
         target: None,
     };
 
-    try!(ops::compile(&root, compile_opts).map(|_| None::<()>).map_err(|err| {
+    let test_executables = try!(ops::compile(&root, compile_opts).map_err(|err| {
         CliError::from_boxed(err, 101)
     }));
 
     let test_dir = root.dir_path().join("target").join("test");
 
-    let mut walk = try!(fs::walk_dir(&test_dir).map_err(|e| {
-        CliError::from_error(e, 1)
-    }));
-
-    for file in walk {
-        // TODO: The proper fix is to have target knows its expected
-        // output and only run expected executables.
-        if file.display().to_string().as_slice().contains("dSYM") { continue; }
-        if !is_executable(&file) { continue; }
-
-        try!(util::process(file).exec().map_err(|e| {
+    for file in test_executables.iter() {
+        try!(util::process(test_dir.join(file.as_slice())).exec().map_err(|e| {
             CliError::from_boxed(e.box_error(), 1)
         }));
     }
 
     Ok(None)
 }
-
-fn is_executable(path: &Path) -> bool {
-    if !path.is_file() { return false; }
-    path.stat().map(|stat| stat.perm.intersects(UserExecute)).unwrap_or(false)
-}
index 427616b9da70984e24bb36127482610d8d1806c4..9fdf73b2010a9c0c93285cc12728fd7b4e148ecd 100644 (file)
@@ -38,7 +38,7 @@ pub struct CompileOptions<'a> {
     pub target: Option<&'a str>,
 }
 
-pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult<()> {
+pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult<Vec<String>> {
     let CompileOptions { update, env, shell, jobs, target } = options;
     let target = target.map(|s| s.to_string());
 
@@ -88,7 +88,18 @@ pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult<()>
     try!(ops::compile_targets(env.as_slice(), targets.as_slice(), &package,
          &PackageSet::new(packages.as_slice()), &resolve, &mut config));
 
-    Ok(())
+    let test_executables: Vec<String> = targets.iter()
+        .filter_map(|target| {
+            if target.get_profile().is_test() {
+                debug!("Run  Target: {}", target.get_name());
+                Some(target.get_name().to_string())
+            } else {
+                debug!("Skip Target: {}", target.get_name());
+                None
+            }
+    }).collect();
+
+    Ok(test_executables)
 }
 
 fn source_ids_from_config() -> CargoResult<Vec<SourceId>> {
index ed2136ab5594b689f2f4959cd46db61256fd9ac1..85129fa1f31d0dfb1b01ebda10d7d319e28d2898 100644 (file)
@@ -222,3 +222,20 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured";
     assert!(out == format!("{}\n\n{}\n\n\n{}\n\n", head, internal, external).as_slice() ||
             out == format!("{}\n\n{}\n\n\n{}\n\n", head, external, internal).as_slice());
 })
+
+test!(dont_run_examples {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", r#"
+        "#)
+        .file("examples/dont-run-me-i-will-fail.rs", r#"
+            fn main() { fail!("Examples should not be run by 'cargo test'"); }
+        "#);
+    assert_that(p.cargo_process("cargo-test"),
+                execs().with_status(0));
+})